<?php
/*
* 团购函数集
*/


/*
* 插入团购
* @arg = array(
*	'site_id' = 
*	'blog_id' = 
*	 title    = 标题
*	 image    = 图片
*	 begin_time =开始时间
*    end_time   = 结束时间
*    tgdescribe = 团购说明
*    market_price = 定价
*    now_price	  = 当前价
*	 other_website = 投放在其他网站的团购 array(
*											name 网站名称
*											url  网站地址
*											)
*	discount		= 折扣
*	content		    = 内容
* )
*   return bool 
*/
function insertTg($arg=array()){
	global $wpdb;
	$defults = array(
		'site_id' 		=> getCurrentSiteId(),
		'blog_id' 		=> getCurrentBlogId(),
		'title'	  		=> false,
		'image'	  		=> false,
		'begin_time'	=> date("Y-m-d H:i:s"),
		'end_time'		=> false,
		'tgdescribe'		=>false,
		'market_price'  => false,
		'now_price'		=> false,
		'other_website'	=> false,
		'discount'		=> false,
		'content'		=> false,
	);
	$args = array_merge($defults,$arg);
	
	extract($args);
	
	$other_website_real= is_array($other_website)?serialize($other_website):$other_website;
	
 $sql='insert into 	cy_tuangou(site_id,blog_id,title,image,begin_time,end_time,tgdescribe,market_price,now_price,other_website,discount,content) values(
					'.$site_id.','.$blog_id.',
					\''.$title.'\',
					\''.$image.'\',
					\''.$begin_time.'\',
					\''.$end_time.'\',
					\''.$tgdescribe.'\',
					\''.$market_price.'\',
					\''.$now_price.'\',
					\''.$other_website_real.'\',
					\''.$discount.'\',
					\''.$content.'\')';
	
	return $wpdb->query($sql);
	//print_r($args);
}
/*
* 更新团购
* @arg = array(
*	 id       = 需要更新的id,如果没有就插入一条数据
*	 title    = 标题
*	 image    = 图片
*	 begin_time =开始时间
*    end_time   = 结束时间
*    tgdescribe = 团购说明
*    market_price = 定价
*    now_price	  = 当前价
*	 other_website = 投放在其他网站的团购 array(
*											name 网站名称
*											url  网站地址
*											)
*	discount		= 折扣
*	content		    = 内容
* )
*   return bool 
*/
function updateTg($arg = array()){
	global $wpdb;
	if(!isset($arg['id']))
		return insertTg($arg);
	
	$defults = selectTgById($arg['id']);
	if(empty($defults))
		return false;	
	
	$args = array_merge((array)$defults,$arg);	
	$args['other_website']  = array_merge((array)$defults->other_website,(array)$arg['other_website']);
	
	extract($args);
	
	$other_website_real= is_array($other_website)?serialize($other_website):$other_website;
	
	$sql = 'update cy_tuangou set title = \''.$title.'\', 
								  image = \''.$image.'\',
								  begin_time = \''.$begin_time.'\',
								  end_time = \''.$end_time.'\',
								  tgdescribe = \''.$tgdescribe.'\',
								  market_price = \''.$market_price.'\',
								  now_price = \''.$now_price.'\',
								  other_website = \''.$other_website_real.'\',
								  discount = \''.$discount.'\',
								  content = \''.$content.'\' where id = '.$id;
	return $wpdb->query($sql);
}
/*
*  根据id查询tg信息
*  @id  需要查询的id的名称
*  return 查询的结果,一个对象
*/
function selectTgById($id){
	global $wpdb;
	$id = intval($id);
	$sql = "select * from cy_tuangou where id = {$id} limit 0,1 ";
	$results  = $wpdb->get_row($sql);
	if(!empty($results))
		$results->other_website = unserialize($results->other_website);	
	return $results;
	//return $wpdb->row($sql);
}
/*
*  获取当前站点的的Tg信息
*  @page 		当前是第几页
*  @par_page    每页显示多少
*  return 查询的结果,一个对象
*/
function getCurrentBlogTg($page,$per_page){
	global $wpdb;
	$site_id = getCurrentSiteId();
	$blog_id = getCurrentBlogId();
	$top = getCurrentTgTop();
		//当前是在第一页
	if($page == 1){
		//只有在第一页才需要查询头部
		if($top){
		  $sql = "select * from cy_tuangou where site_id = {$site_id} and blog_id = {$blog_id} and id != {$top} limit 0,".($per_page-1)." order by by_order";
		  $topResults = selectTgById($top);
		  $otherResults = $wpdb->get_results($sql);
		  return array_unshift($otherResults,$topResults);
		}
	}
	//其他的sql
	$limit = ($page-1)*$per_page;
	if($top){
$sql ="select * from cy_tuangou  where site_id={$site_id} and blog_id = {$blog_id} and id != {$top} limit {$limit},{$per_page} order by by_order";	
	}else{
$sql ="select * from cy_tuangou  where site_id={$site_id} and blog_id = {$blog_id} limit {$limit},{$per_page} order by by_order";
	}
	$results = $wpdb->get_results($sql);
	if(!$results)
		return false;
	foreach($results as $key=>$val){	
		$results[$key]->other_website = unserialize($val->other_website);
	}
	//$results->other_website = unserialize($results->other_website);
	return $results;
}
/*
* 获取推荐在头部第一个的团购信息
* 返回一个团购的id或者false
*/
function getCurrentTgTop(){
	return false;	
}

/*上传团购图片*/
function tuangou_upload_url(){
	global $current_blog;
	$opus_dir = UPLOAD_DIR;
	$opus_url =UPLOAD_URL;
	$bdir = $opus_dir;
	$burl = $opus_url;
	$subdir="/".getCurrentSiteIdReal().'_'.getCurrentBlogIdReal().'/tuangou';	
	$opus_dir .= $subdir;
	$opus_url .= $subdir;
	$dir=$opus_dir;
	$url=$opus_url;
	return array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'basedir' => $bdir, 'baseurl' => $burl, 'error' => false );	
}
function tuangou_upload($file){
	$arg=array(
		'get_new_filename'=>'sales_upload_name',
		'get_new_url'	=>'tuangou_upload_url',
		'type_arr'	=>array('jpg','gif','png','jpeg'),
		'file'	=>$file
	);
	$result=image_upload($arg);
	$file_arr=explode("/",$result['file']);
	$result['filename']=$file_arr[count($file_arr)-1];
	return $result;	
}